package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单管理相关接口")
@Slf4j
@RestController("adminOrderController")
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;


    /**
     * 订单分页
     * @param dto
     * @return
     */

    @ApiOperation("分页查询")
    @GetMapping("/conditionSearch")
    public Result getOrderPageQuery(OrdersPageQueryDTO dto){
        log.info("订单查询分页参数："+dto);
        PageResult orderList = orderService.getOrder(dto);
        return Result.success(orderList);
    }


    /**
     * 订单状态改成3（待派送）
     * @param dto
     * @return
     */
    @ApiOperation("接单-订单状态改成3（待派送）")
    @PutMapping("/confirm")
    public Result receivOrdres(@RequestBody OrdersConfirmDTO dto){
        log.info("状态id："+dto);
        orderService.updateStatus(dto);
       return Result.success();
    }


    /**
     * 根据订单id查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("根据订单id查询订单详情")
    @GetMapping("/details/{id}")
    public Result getOrderDetailById(@PathVariable Integer id){
        log.info("订单id："+ id);
        OrderVO OrderVO = orderService.getOrderDetailById(id);
        return Result.success(OrderVO);//OrderVO
    }


    //派单

    /**
     * 订单状态修改为4（派送中）
     * @param id
     * @return
     */
    @ApiOperation("派单-订单状态修改为4（派送中）")
    @PutMapping("/delivery/{id}")
    public Result dispatchOrders(@PathVariable Long id){
        log.info("订单id："+id);
        orderService.updateDispatchOrdersStatus(id);
        return Result.success();
    }


    /**
     * 订单状态修改为5（完成）
     * @param id
     * @return
     */
    @ApiOperation("派单-订单状态修改为5（完成）")
    @PutMapping("/complete/{id}")
    public Result completeOrders(@PathVariable Long id){
        log.info("订单id："+id);
        orderService.updateCompleteOrdersStatus(id);
        return Result.success();
    }


    /**
     * 订单取消-订单状态修改为6（取消）
     * @param dto
     * @return
     */

    @ApiOperation("订单取消-订单状态修改为6（取消）")
    @PutMapping("/cancel")
    public Result cancelOrders(@RequestBody OrdersCancelDTO dto) throws Exception {
        log.info("状态id："+dto);


        //修改状态
        orderService.updateCancelOrdersStatus(dto);

        return Result.success();
    }



    @ApiOperation("订单拒绝-订单状态修改为6（取消）")
    @PutMapping("/rejection")
    public Result rejectionOrder(@RequestBody OrdersRejectionDTO dto){
        log.info("拒绝订单信息："+dto);
        orderService.updateRejectionOrder(dto);
        //TODO 退款
        return Result.success();
    }


    /**
     * 订单状态数量统计
     * @return
     */
    @ApiOperation("订单状态数量统计")
    @GetMapping("/statistics")
    public Result statisticsOrders(){
        log.info("订单统计查询：");
        OrderStatisticsVO statisticsVO = orderService.statisticsOrders();
        return Result.success(statisticsVO);
    }







}
